using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Mes.质检管理.ProductCheck
{
    public partial class ProductCheckAdd : Form
    {
        private const string ADD_URL = "/api/Inspection/AddProductInspection";

        public ProductCheckAdd()
        {
            InitializeComponent();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var payload = new
                {
                    // 质检基本信息
                    InspectionNo = txtInspectionNo.Text,
                    InspectionName = txtInspectionName.Text,
                    InspectionDate = dtInspectionDate.Value.ToString("yyyy-MM-dd"),
                    Inspector = txtInspector.Text,
                    Supplier = txtSupplier.Text,

                    // 关联Id，可按需对接真实来源
                    MaterialInfoId = 0,
                    ProductId = 0,
                    InventoryInfoId = 0,
                    InspectionDetailId = 0,

                    // 产品工艺工序信息
                    ProductNo = txtProductNo.Text,
                    ProductName = txtProductName.Text,
                    Specification = txtSpecification.Text,
                    ProductionLineNo = txtLineNo.Text,
                    ProcessRouteNo = txtRouteNo.Text,
                    ProcessRouteName = txtRouteName.Text,
                    ProcessStepNo = txtStepNo.Text,
                    ProcessStepName = txtStepName.Text,

                    // 质检明细
                    InspectionQuantity = ToInt(txtQty.Text),
                    QualifiedQuantity = ToInt(txtQualified.Text),
                    UnqualifiedQuantity = ToInt(txtUnqualified.Text),
                    InspectionResult = cboResult.Text,
                    Inspectordetal = txtDetail.Text,
                    Remarks = txtRemarks.Text,
                    InspectionTableId = 0
                };

                var json = JsonConvert.SerializeObject(payload);
                using (var client = new HttpClient())
                {
                    // 设置超时时间
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(HttpClientHelper.GetApiUrl("/api/Inspection/AddProductInspection"), content);
                    if (response.IsSuccessStatusCode)
                    {
                        MessageBox.Show("新增成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                        return;
                    }
                    else
                    {
                        // 处理错误响应
                        string errorContent = await response.Content.ReadAsStringAsync();
                        MessageBox.Show($"保存失败：{response.StatusCode} - {errorContent}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private int ToInt(string t)
        {
            int v; return int.TryParse(t, out v) ? v : 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}


